package com.emep.changzhi.analyse.http.basics;


import com.emep.changzhi.analyse.entity.basics.EPA;
import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.entity.basics.SysEmployee;
import com.emep.changzhi.analyse.entity.basics.SysMonitorSiteInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysEmployeeService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 系统员工信息
 */

@RestController
@RequestMapping("staff")
@Api(value="系统员工信息controller",tags={"系统员工信息"})
public class SysEmployeeController {

    @Autowired
    private SysEmployeeService service;
    MapToObject<SysEmployee> mapToObject=new MapToObject<>();
    /**
     * 新增系统工作人员信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增系统员工信息", notes="新增系统员工信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "add")
    public ResultBean<SysEmployee> add(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "staff");
        if(result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Map<String, Object> map = (Map<String, Object>) payload.get("staff");
            SysEmployee entity= new SysEmployee();
            boolean b= mapToObject.convertEntity(entity,map);
            if (b){
                boolean flag= service.save(entity);
                if(flag){
                    return result.boolAdd("新增成功");
                }else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数失败");
            }


        }
        return  result;
    }

    /**
     * 修改员工信息
     * @param payload
     * @return
     */
    @ApiOperation(value="修改员工信息", notes="修改员工信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "weixin", value = "微信号", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "deppartid", value = "部门id", required = true, dataType = "Integer",paramType="query"),
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String",paramType="query")

    })
    @PostMapping(value = "update")
    public ResultBean update(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "staff");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("staff");
            if (!map.containsKey("staffId")) {
                return result.error("staffId为必传字段");
            } else {
                Integer id = Integer.parseInt(map.get("staffId").toString());
                SysEmployee entity = service.findById(id);
                if (entity != null) {
                    boolean b = mapToObject.convertEntity(entity, map);
                    if (entity != null) {
                        if (b) {
                            boolean a = service.update(entity);
                            if (a) {
                                return result.boolAdd("修改成功");
                            } else {
                                return result.boolAdd("该名称已存在");
                            }
                        } else {
                            return result.error("参数错误");
                        }
                    } else {
                        return result.boolAdd("无此员工");
                    }
                }

            }
        }
        return result;
    }


    /**
     * 根据id 删除系统员工信息
     * @param payload
     */
    @ApiOperation(value="删除系统员工信息", notes="根据id删除系统员工信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "/delete")
    public ResultBean delete(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "staffId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            boolean b=service.delete(Integer.parseInt(payload.get("staffId").toString()));
            if(b){
                return result.boolAdd("删除成功");
            }else {
                return result.error("删除失败");
            }

        }
        return result;
    }

    /**
     * 查询所有系统员工信息
     * @return
     */
    @ApiOperation(value="查询所有系统员工信息", notes="无参数查询系统员工信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "list")
    public ResultBean<List<SysEmployee>> findAllByDepartment(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<SysEmployee> industryList = service.find(pageIndex, pageSize, condition);
            PageChunk<SysEmployee> pageChunk = new PageChunk<>();
            if(industryList != null){
                return result.boolAdd(pageChunk.pageChunk(industryList));
            }else {
                return result.error("未查到员工列表");
            }
        }
        return result;
    }

    /**
     * 通过id 根据id查询系统员工信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询系统员工信息", notes="根据id查询系统员工信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @PostMapping(value = "info")
    public ResultBean<SysEmployee> findAreaById(@RequestBody Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "staffId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {

            SysEmployee employee =service.findById(Integer.parseInt(payload.get("staffId").toString()));
            if(employee != null){
                return result.boolAdd(employee);
            }else {
                return result.error("未查到员工列表");
            }
        }
        return result;
    }



}
